package vista;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import constants.Constants;
/**
 * this class will let us to create a Pannel to iterate with the 
 * game cards and to let us know witch cards do we have
 * @author Jose Luis Garcia && Alvaro Blazquez Checa
 *
 */
public class CardsPanel extends JFrame implements ActionListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private DrawingPanel panel;
	private JButton closeButton;
	private List<Image> monkImages;
	private static final int cardWidth = 87;
	private static final int cardHeight = 160;
	
	/**
	 * this will create the Card Panel zone in the Main Gui 
	 * @param monkImagesParam the list of all monks to show in the panel when selected
	 * @param bounds the size for our panel
	 */
	public CardsPanel(final List<Image> monkImagesParam, Rectangle bounds){
		
		this.setBounds(bounds);
		this.setUndecorated(true);
		this.setAlwaysOnTop(true);

		this.monkImages = new ArrayList<Image>();
		scaleImages(monkImagesParam);
		
		panel = new DrawingPanel();
		panel.setBorder(Constants.doubleBorder);
		panel.setLayout(null);
		setContentPane(panel);

		closeButton = new JButton("X");
		closeButton.setSize(60, 30);
		closeButton.setLocation(getWidth()/2 - closeButton.getWidth()/2, getHeight() - closeButton.getHeight()-10);
		closeButton.addActionListener(this);
		closeButton.setBackground(Color.BLACK);
		closeButton.setForeground(Color.RED);
		panel.add(closeButton);
	}
	
	/**
	 * this method will let us hide the panel when the user make a mouse click
	 */	
	@Override
	public void actionPerformed(ActionEvent e) {
		setVisible(false);
	}
	
	/**
	 * this method will change the size of our images to make them change to the size of 
	 * the card for been added to the panel
	 * @param monkImagesParam
	 */
	private void scaleImages(final List<Image> monkImagesParam) {
		if(monkImagesParam!=null) 
			for(Image image:monkImagesParam) 
				monkImages.add(image.getScaledInstance(87, 160, BufferedImage.SCALE_SMOOTH));
	}
	
	/**
	 * this private class will add a empty zone to keep the cards 
	 * @author Jose Luis Garcia && Alvaro Blazquez Checa
	 *
	 */
	private class DrawingPanel extends JPanel {
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public DrawingPanel() { }
		/**
		 * this method will add cards in relative position to each others 
		 */
		public void paintComponent(Graphics g) {

			g.setColor(Color.DARK_GRAY);
			g.fillRect(0, 0, getWidth(), getHeight());
			int j = 0;
			for (int i =0 ; i < monkImages.size();i++) {
				int posX = (j % 6) * (10 + cardWidth)+ 13;
				int posY = (j / 6) * (10 + cardHeight)+ 13;
				if(GameLogic.monkCards[i]) {
					j++;
					g.drawImage(monkImages.get(i), posX, posY, cardWidth, cardHeight,null);
				}
			}	
		}
		
	}
	
}
